package view.components
{
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import utils.ColorTool;
	import utils.animate.TweenLight;
	import utils.animate.TweenProperty;
	
	public class CountNumber extends TextField
	{
		public static const GOLD:int = 0;
		public static const GREEN:int = 1;
		
		
		
		private var colorType:int;
		private var alphaTween:TweenProperty;
		private var yTween:TweenLight;
		private var size:int;
		
		public function CountNumber(type:int,size:int = 30)
		{
			this.mouseEnabled = false;
			this.selectable = false;
			colorType = type;
			this.size = size;
			initText();
			initFilter();
			initTween();
		}
		
		private function initText():void
		{
			var tf:TextFormat = this.defaultTextFormat;
			tf.size = size;
			tf.color = 0xFFFFFF;
			tf.font = "Arial";
			this.defaultTextFormat = tf;
		}
		
		private function initTween():void
		{
			alphaTween = new TweenProperty();
			alphaTween.duration = 1;
			yTween = new TweenLight();
			yTween.duration = 1;
			alphaTween.addEventListener(Event.COMPLETE,onComplete);
			yTween.addEventListener(Event.COMPLETE,onComplete);
		}
		
		private function onComplete(event:Event):void
		{
			if (event.target == yTween)
			{
				alphaTween.start();
			}
			else
			{
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		public function show(content:String,fy:int,ty:int):void
		{
			if (content)text = content;
			width = textWidth + 20;
			alphaTween.setTarget(this,"alpha",1,0.5);
			yTween.setTarget(this,this.x,fy,this.x,ty);
			
			yTween.start();
		}
		
		private function initFilter():void
		{
			switch(colorType)
			{
				case GOLD:
					ColorTool.glowGold(this);
					break;
				case GREEN:
					ColorTool.glowGreen(this);
					break;
			}
			//ColorTool.addShadow(this);
		}
		
		public function destroy():void
		{
			alphaTween.removeEventListener(Event.COMPLETE,onComplete);
			yTween.removeEventListener(Event.COMPLETE,onComplete);
			yTween.destroy();
			alphaTween.destroy();
			yTween = null;
			alphaTween = null;
			this.filters = null;
			
		}
		
		
	}
}